package com.sjtu.bookread.service;

import com.sjtu.bookread.obj.BookListObj;
import com.sjtu.bookread.obj.BookObj1;
import com.sjtu.bookread.obj.ChapterObj1;
import com.sjtu.bookread.obj.LoginObj;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RecomService {
    public static String ApiUrl = "http://ligongzzz.top:6777";

    private static RecomService recomService = new RecomService("root");

    private String token;

    private BookObj1 bookObj1;

    private BookListObj bookListObj;

    private ChapterObj1 chapterObj1;

    private LoginObj loginObj;

    OkHttpClient build = new OkHttpClient.Builder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(2,TimeUnit.SECONDS)
            .writeTimeout(2,TimeUnit.SECONDS)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(build)
            .build();

    private UrlService1 ApiService = retrofit.create(UrlService1.class);


    public static synchronized RecomService getRecomService() {return recomService;}

    public RecomService(String _token) {
        token = _token;
        recomService = this;

    }

    //登录
    public LoginObj getLogin(String username, String password) {
        Response<LoginObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            response = ApiService.GetLogin(requestBody).execute();
            loginObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (loginObj != null && loginObj.getType().equals("0")) {
            recomService = new RecomService(loginObj.getToken());
        }
        return loginObj;
    }

    //注册
    public LoginObj getRegister(String username, String password) {
        Response<LoginObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            response = ApiService.GetRegister(requestBody).execute();
            loginObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return loginObj;
    }

    // 获取推荐书单
    public BookListObj getRecomBookList() {
        Response<BookListObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("token",token);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            response = ApiService.GetRecom(requestBody).execute();
            bookListObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookListObj;
    }

    //获取排行榜
    public BookListObj getRankingBookList() {
        Response<BookListObj> response = null;
        try{
            response = ApiService.GetHotbook().execute();
            bookListObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookListObj;
    }

    //获取书籍
    public BookObj1 getBook(int id) {
        Response<BookObj1> response = null;
        HashMap<String,Integer> map = new HashMap<>();
        map.put("book_id",id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            response = ApiService.GetBook(requestBody).execute();
            bookObj1 = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookObj1;
    }

    //获取书籍章节内容
    public ChapterObj1 getChapter(int id, int cur_chapter) {
        Response<ChapterObj1> response = null;
        HashMap<String,Integer> map = new HashMap<>();
        map.put("book_id",id);
        map.put("chapter_num",cur_chapter);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());

        try {
            response = ApiService.GetChapter(requestBody).execute();
            chapterObj1 = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chapterObj1;
    }

    //获取书架内容
    public BookListObj getShelfBookList() {
        Response<BookListObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("token",token);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            response = ApiService.GetShelf(requestBody).execute();
            bookListObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookListObj;
    }

    //获取搜索结果
    public BookListObj getSearchBookList(String search) {
        Response<BookListObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("search",search);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            response = ApiService.Search(requestBody).execute();
            bookListObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookListObj;
    }

    //获取特殊搜索结果
    public BookListObj getSpecialSearchBookList(String book_id) {
        Response<BookListObj> response = null;
        HashMap<String,String> map = new HashMap<>();
        map.put("book_id",book_id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            response = ApiService.SpecialSearch(requestBody).execute();
            bookListObj = response.body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookListObj;
    }

    public void adddelShelfbook(int type, int id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("type",type);
        map.put("book_id",id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try{
            ApiService.ModifyShelf(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_in_reader(int id, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_reader");
        map.put("token",token);
        map.put("book_id",id);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_reader(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_in_detail(int id, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_info");
        map.put("token",token);
        map.put("book_id",id);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_info(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_recom_stay(int id, float recom_stay_time, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_stay");
        map.put("token",token);
        map.put("book_id",id);
        map.put("recom_stay",recom_stay_time);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_stay(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_read_speed(int id, List<Float> reader_speed, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_speed");
        map.put("token",token);
        map.put("book_id",id);
        map.put("reader_speed",reader_speed);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_speed(requestBody).execute();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_rate(int id, int rate, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_rate");
        map.put("token",token);
        map.put("book_id",id);
        map.put("rate",rate);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_rate(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void submit_zan_cai(int id, int great_or_not, float ax, float ay, float az) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("post_type","view_great");
        map.put("token",token);
        map.put("book_id",id);
        map.put("great_or_not",great_or_not);
        map.put("ax",ax);
        map.put("ay",ay);
        map.put("az",az);
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),new JSONObject(map).toString());
        try {
            ApiService.Submit_view_great(requestBody).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
